package gac.helloworld;

import gac.helloworld.MyLocation.LocationResult;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LocateService extends Service {

	private Timer timer = new Timer();
	private static long UPDATE_INTERVAL = 60000;
	public long delay = 600000;
	public static MainActivity MAIN_ACTIVITY;
	private MapHandlerActivity map;

	public static void setMainActivity(MainActivity activity) 
	{
		MAIN_ACTIVITY = activity;
	}

	public void onCreate() {
		super.onCreate();
		map = new MapHandlerActivity(MAIN_ACTIVITY);
		this.startService();
	}

	private int getEndTimeInSecs(int hour, int minute)
	{
		return hour * 60 * 60 + minute * 60;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Bundle bundle = intent.getExtras(); 
		if (intent != null && bundle != null)
		{
			int warntime = bundle.getInt(AlarmDbAdapter.KEY_WARNTIME);
			int hour = bundle.getInt(AlarmDbAdapter.KEY_HOUR);
			int minute = bundle.getInt(AlarmDbAdapter.KEY_MINUTE);
			String dest = bundle.getString(AlarmDbAdapter.KEY_DESTINATION);

			map.setEndTime(getEndTimeInSecs(hour, minute));
			map.setBufferTime(warntime*60); // convert mins to secs
			MapHandlerActivity.findCurrentLocation();
			Log.v("destination", dest);
			map.setFinalDestination(dest);

		}
	}	

	public void onDestroy(){
		super.onDestroy();
		this.shutdownService();
	}

	public void shutdownService()
	{
		if (timer != null) timer.cancel();
		Log.i(getClass().getSimpleName(), "Timer stopped...");
		this.stopSelf();
	}

	public IBinder onBind(Intent intent) {
		return null;
	}

	private void startService() {
		getUpdate();
		/*
		timer.scheduleAtFixedRate(
				new TimerTask() {
					public void run() {
						getUpdate();
					}
				},0,UPDATE_INTERVAL);
		 */
		Log.i(getClass().getSimpleName(), "Timer started!!!");
	}

	private void getUpdate(){
		map.calculateTime();
		Log.v("LocateService", "Updated");
		if(map == null){
			Log.v("","Map is null");
		}
		if(map.isTimeToLeave()){
			String ns = Context.NOTIFICATION_SERVICE;
			NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
			int icon = R.drawable.icon;
			CharSequence tickerText = "It's time to leave!";
			long when = System.currentTimeMillis();

			Notification notification = new Notification(icon, tickerText, when);
			notification.defaults |= Notification.DEFAULT_SOUND;
			Context context = getApplicationContext();
			CharSequence contentTitle = "It's time to leave!";
			CharSequence contentText = "";
			Intent i = new Intent(getApplicationContext(), AlarmNotificationActivity.class);
			i.putExtra(AlarmDbAdapter.KEY_DESTINATION, map.getFinalDestination());
			PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

			notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
			mNotificationManager.notify(1, notification);
			
		}
		else{
			if(map.getLeaveTimeInterval()<600){
				delay = 60000;
			}
			else if(map.getLeaveTimeInterval()>24*60*60){
				delay = 60*60*1000;
			}
			else{
				delay = Math.min(60*60*1000, map.getLeaveTimeInterval()*100);
			}
			timer.schedule(new TimerTask() {
				public void run() {
					getUpdate();
				}
			}, delay);
		}
	}


}
